/********************************************************************
Name: Figure 2: Republican and Democratic Sheriffs Comply with 
	ICE Detainer Requests at the Same Rate
Author: Dan Thompson
Date: August 2019
Notes:
********************************************************************/

* Set the working directory 
gl root = "$sanctuary_root"
gl root = "YOUR_WORKING_DIRECTORY"

* Bring in the analysis data
use "$root/data/rd analysis data.dta", clear

* Get the CEF from the regression 
reg share_detained_sheriff treat rv rv2 rv3 treat_rv treat_rv2 treat_rv3 ///
	if abs(rv)<25, vce(clust county_id)
predict y_hat if abs(rv)<25 & share_detained_sherif!=.
predict pred_error if abs(rv)<25 & share_detained_sherif!=., stdp
keep share_detained_sheriff y_hat rv treat pred_error
keep if abs(rv)<25 & share_detained_sherif!=.

* Construct the standard errors
gen pred_err_lower = y_hat - 1.96*pred_error
gen pred_err_upper = y_hat + 1.96*pred_error

* Make the bin means for the plot
gsort -treat rv
gen row = _n if treat==1
gsort treat -rv
replace row = _n if treat==0
gen bin = ceil(row/100)
egen mean_share = mean(share), by(bin treat)
egen mean_rv = mean(rv), by(bin treat)

* Convert the running variable back to Dem vote share
gen dem_vs = rv+50
gen mean_dem_vs = mean_rv+50

* Run the plot
twoway (scatter share dem_vs, mc(gs10) msi(vtiny)) ///
	(scatter mean_share mean_dem_vs, mc(gs7) msi(small)) ///
	(line pred_err_upper dem_vs if !treat, lc(gs10) lp(dash)) ///
	(line pred_err_upper dem_vs if treat, lc(gs10) lp(dash)) ///
	(line pred_err_lower dem_vs if !treat, lc(gs10) lp(dash)) ///
	(line pred_err_lower dem_vs if treat, lc(gs10) lp(dash)) ///
	(line y_hat dem_vs if !treat, lc(dknavy)) ///
	(line y_hat dem_vs if treat, lc(dknavy)), ///
	xline(50, lc(gs5)) ///
	legend(off) ///
	xsc(r(25 75)) xlab(30(10)70, labs(medlarge)) ///
	ysc(r(0 1)) ylab(0(.20)1, labs(medlarge)) ///
	yti("Compliance Rate", si(medlarge)) ///
	xti("Dem Vote Share (%)", si(medlarge)) ///
	bgcolor(white) graphregion(color(white))
